package com.ssy.controller;

import com.ssy.anno.Log;
import com.ssy.entity.Dept;
import com.ssy.entity.Result;
import com.ssy.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 查询所有部门
     * @return
     */

    @GetMapping
    public Result listAll() {
        log.info("查询所有部门");
        List<Dept> deptList = deptService.listAll();
        return Result.success(deptList);
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @Log
    @DeleteMapping
    public Result deleteDept(Integer id) {
        log.info("删除部门 {}", id );
        deptService.deleteByID(id);
        return Result.success();
    }


    /**
     * 添加部门
     */
    @Log
    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        log.info("添加部门 {}", dept);
        deptService.addDept(dept);
        return Result.success();
    }

    /**
     * 根据id查询部门(修改部门查询回显功能)
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询部门 {}", id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    /**
     * 修改部门
     */
    @Log
    @PutMapping
    public Result updateDept(@RequestBody Dept dept) {
        log.info("修改部门 {}", dept);
        deptService.updateDept(dept);
        return Result.success();
    }

}
